package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.form.model.FormTable;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.dao.BpmBusLinkDao;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.bpm.BpmBusLink;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ClassDescribe:业务数据关联表 Service
 * @author :zks
 * Date: 2019-09-04
 * Since:1
 */
@Service
public class BpmBusLinkService {

    @Resource
    private BpmBusLinkDao bpmBusLinkDao;

    @Transactional(rollbackFor = Exception.class)
    public void addBusLink(String pk, ProcessRun processRun, FormTable formTable){
        boolean isNumber= FormTable.PKTYPE_NUMBER.equals(formTable.getKeyDataType());
        add(pk, processRun, isNumber, formTable.getTableName());
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(String pk,ProcessRun processRun,boolean isNumber,String tableName) {

        SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
        SysOrg curOrg = (SysOrg) ContextUtil.getCurrentOrg();
        String id = UUIDUtils.getUUIDFor32();
        BpmBusLink busLink = new BpmBusLink();
        busLink.setBusId(id);

        if (isNumber) {
            busLink.setBusPk(Long.parseLong(pk));
        } else {
            busLink.setBusPkstr(pk);
        }

        //加入表明 (分区字段)
        if (StringUtil.isNotEmpty(tableName)) {
            //不再操作分区，因为mysql、sqlserver数据库中并没有设置分区，且程序并未按照分区进行查询
            busLink.setBusFormTable(tableName);
        } else {
            busLink.setBusFormTable(BpmBusLink.TABLE_UNCREATED);
        }

        busLink.setBusCreatorId(curUser.getUserId());
        busLink.setBusCreator(curUser.getFullName());
        busLink.setBusCreatetime(new Date());
        if (BeanUtils.isNotEmpty(curOrg)) {
            busLink.setBusOrgName(curOrg.getOrgName());
            busLink.setBusOrgId(curOrg.getOrgId());
        }
        busLink.setBusUpdid(curUser.getUserId());
        busLink.setBusUpd(curUser.getFullName());
        busLink.setBusUpdtime(new Date());

        if (processRun != null) {
            busLink.setBusDefId(processRun.getProcDefId());
            busLink.setBusFlowRunid(processRun.getRunId());
            busLink.setBusStatus(BpmBusLink.BUS_STATUS_RUNNING);

        } else {
            busLink.setBusStatus(BpmBusLink.BUS_STATUS_BUSINESS);
        }
        bpmBusLinkDao.add(busLink);
    }
    /**
     * 更新业务关联数据。
     * @param pk 主键
     * @param bpmformtable 表单表
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBusLink(String pk,ProcessRun processRun, FormTable bpmformtable){
        SysUser curUser=(SysUser) ContextUtil.getCurrentUser();
        BpmBusLink link;
        if(bpmformtable.isExtTable()){
            if (FormTable.PKTYPE_NUMBER.equals(bpmformtable.getKeyDataType())){
                link=bpmBusLinkDao.getByPk(Long.parseLong(pk));
            }
            else{
                link=bpmBusLinkDao.getByPkStr(pk);
            }
        }
        else{
            link=bpmBusLinkDao.getByPkStr(pk);
        }
        if(BeanUtils.isNotEmpty(link)){
            if(processRun==null){
                link.setBusStatus(BpmBusLink.BUS_STATUS_BUSINESS);
            }
            else{
                link.setBusStatus(BpmBusLink.BUS_STATUS_RUNNING);
            }
            link.setBusUpdid(curUser.getUserId());
            link.setBusUpd(curUser.getFullName());
            link.setBusUpdtime(new Date());
            bpmBusLinkDao.update(link);
        }
    }

    /**
     * 删除关联数据记录。
     * @param pk 主键
     * @param bpmformtable 表单表
     */
    @Transactional(rollbackFor = Exception.class)
    public void delBusLink(String pk,FormTable bpmformtable){
        if(bpmformtable.isExtTable()){
            if (FormTable.PKTYPE_NUMBER.equals(bpmformtable.getKeyDataType())){
                bpmBusLinkDao.delByPk(Long.parseLong(pk));
            }
            else{
                bpmBusLinkDao.delByPkStr(pk);
            }
        }
        else{
            bpmBusLinkDao.delByPkStr(pk);
        }

    }

}
